package com.tms.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tms.entity.*;
import com.tms.service.OrderService;
import com.tms.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * (Stock)表控制层
 *
 * @author makejava
 * @since 2024-12-27 23:18:03
 */
@RestController
@RequestMapping("stock")
public class StockController {
    @Autowired
    private StockService stockService;


    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Stock stock) {
        return stockService.save(stock) ? Result.success() : Result.error();
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Stock stock) {
        return stockService.updateById(stock) ? Result.success() : Result.error();
    }

    //删除
    @DeleteMapping("/del")
    public Result del(@RequestParam String id) {
        return stockService.removeById(id) ? Result.success() : Result.error();
    }

    @GetMapping ("/pageList")
    public Result pageList(@RequestBody PageBean pageBean) {
        HashMap params = pageBean.getParams();

        String bookId = params.get("bookId").toString();

        Page<Stock> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());

        LambdaQueryWrapper<Stock> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(bookId) && !"null".equals(bookId)) {
            lambdaQueryWrapper.like(Stock::getBookId, bookId);
        }

        IPage result = stockService.pageList(page, lambdaQueryWrapper);

        return Result.success(result.getRecords());
    }

}

